package com.epsilon.pos.principal.ventas.view;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.events.VerifyEvent;

import com.epsilon.commons.util.SWTResourceManager;
import com.epsilon.commons.util.WidgetsUtil;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.events.KeyAdapter;

public class VentasPrincipalCalculadorShell extends Shell {
	private Text	text;
	private Button	btn1;
	private Button	btn2;
	private Button	btn4;
	private Button	btn7;
	private Button	btn8;
	private Button	btn9;
	private Button	btnC;
	private Button	btn0;
	private Button	btnPunto;
	private Button	btnDel;
	private Button	btnAceptar;

	private Float	montoRecibido	= null;

	public VentasPrincipalCalculadorShell( Shell display ) {
		super( display, SWT.CLOSE | SWT.RESIZE | SWT.TITLE | SWT.APPLICATION_MODAL );

		Composite composite = new Composite( this, SWT.NONE );
		composite.setBounds( 0, 0, 236, 407 );

		text = new Text( composite, SWT.BORDER | SWT.CENTER );
		text.setBounds( 10, 10, 216, 39 );
		text.addKeyListener( new KeyListener() {

			@Override
			public void keyPressed( KeyEvent e ) {

				if( WidgetsUtil.esNumero( "" + e.character ) || !text.getText().trim().isEmpty() )
					btnAceptar.setEnabled( true );
				else
					btnAceptar.setEnabled( false );

				switch( e.keyCode ) {
					case WidgetsUtil.TECLA_ESC:
						montoRecibido = null;
						close();
						break;

					case WidgetsUtil.TECLA_ENTER:
						if( !text.getText().isEmpty() ) {
							montoRecibido = Float.parseFloat( text.getText().trim() );
							close();
						}
						break;
				}
			}

			@Override
			public void keyReleased( KeyEvent e ) {
				if( !isDisposed() ) {
					if( WidgetsUtil.esNumero( "" + e.character ) || !text.getText().trim().isEmpty() )
						btnAceptar.setEnabled( true );
					else
						btnAceptar.setEnabled( false );
				}
			}
		} );
		text.addVerifyListener( WidgetsUtil.getVerifyListenerCampoNumericoFlotante( text ) );
		text.addVerifyListener( new VerifyListener() {
			public void verifyText( VerifyEvent ve ) {
				if( !text.getText().trim().isEmpty() || WidgetsUtil.esNumero( ve.text ) )
					btnAceptar.setEnabled( true );
				else
					btnAceptar.setEnabled( false );
			}
		} );
		text.setFont( SWTResourceManager.getFont( "Tahoma", 18, SWT.BOLD ) );

		btn1 = new Button( composite, SWT.NONE );
		btn1.setBounds( 10, 55, 68, 68 );
		btn1.setFont( SWTResourceManager.getFont( "Tahoma", 16, SWT.BOLD ) );
		btn1.addKeyListener( new KeyAdapter() {
			@Override
			public void keyPressed( KeyEvent e ) {
				switch( e.keyCode ) {
					case WidgetsUtil.TECLA_ESC:
						montoRecibido = null;
						close();
						break;

					case WidgetsUtil.TECLA_ENTER:
						if( !text.getText().isEmpty() ) {
							montoRecibido = Float.parseFloat( text.getText().trim() );
							close();
						}
						break;
				}
			}
		} );
		btn1.addSelectionListener( new SelectionAdapter() {
			@Override
			public void widgetSelected( SelectionEvent arg0 ) {
				text.append( "1" );
			}
		} );
		btn1.setText( "1" );

		btn2 = new Button( composite, SWT.NONE );
		btn2.setBounds( 84, 55, 68, 68 );
		btn2.setFont( SWTResourceManager.getFont( "Tahoma", 16, SWT.BOLD ) );
		btn2.addKeyListener( new KeyAdapter() {
			@Override
			public void keyPressed( KeyEvent e ) {
				switch( e.keyCode ) {
					case WidgetsUtil.TECLA_ESC:
						montoRecibido = null;
						close();
						break;

					case WidgetsUtil.TECLA_ENTER:
						if( !text.getText().isEmpty() ) {
							montoRecibido = Float.parseFloat( text.getText().trim() );
							close();
						}
						break;
				}
			}
		} );
		btn2.addSelectionListener( new SelectionAdapter() {
			@Override
			public void widgetSelected( SelectionEvent arg0 ) {
				text.append( "2" );
			}
		} );
		btn2.setText( "2" );

		Button btn3 = new Button( composite, SWT.NONE );
		btn3.setBounds( 158, 55, 68, 68 );
		btn3.setFont( SWTResourceManager.getFont( "Tahoma", 16, SWT.BOLD ) );
		btn3.addKeyListener( new KeyAdapter() {
			@Override
			public void keyPressed( KeyEvent e ) {
				switch( e.keyCode ) {
					case WidgetsUtil.TECLA_ESC:
						montoRecibido = null;
						close();
						break;

					case WidgetsUtil.TECLA_ENTER:
						if( !text.getText().isEmpty() ) {
							montoRecibido = Float.parseFloat( text.getText().trim() );
							close();
						}
						break;
				}
			}
		} );
		btn3.addSelectionListener( new SelectionAdapter() {
			@Override
			public void widgetSelected( SelectionEvent arg0 ) {
				text.append( "3" );
			}
		} );
		btn3.setText( "3" );

		Button btn6 = new Button( composite, SWT.NONE );
		btn6.setBounds( 158, 129, 68, 68 );
		btn6.setFont( SWTResourceManager.getFont( "Tahoma", 16, SWT.BOLD ) );
		btn6.addKeyListener( new KeyAdapter() {
			@Override
			public void keyPressed( KeyEvent e ) {
				switch( e.keyCode ) {
					case WidgetsUtil.TECLA_ESC:
						montoRecibido = null;
						close();
						break;

					case WidgetsUtil.TECLA_ENTER:
						if( !text.getText().isEmpty() ) {
							montoRecibido = Float.parseFloat( text.getText().trim() );
							close();
						}
						break;
				}
			}
		} );
		btn6.addSelectionListener( new SelectionAdapter() {
			@Override
			public void widgetSelected( SelectionEvent arg0 ) {
				text.append( "6" );
			}
		} );
		btn6.setText( "6" );

		Button btn5 = new Button( composite, SWT.NONE );
		btn5.setBounds( 84, 129, 68, 68 );
		btn5.setFont( SWTResourceManager.getFont( "Tahoma", 16, SWT.BOLD ) );
		btn5.addKeyListener( new KeyAdapter() {
			@Override
			public void keyPressed( KeyEvent e ) {
				switch( e.keyCode ) {
					case WidgetsUtil.TECLA_ESC:
						montoRecibido = null;
						close();
						break;

					case WidgetsUtil.TECLA_ENTER:
						if( !text.getText().isEmpty() ) {
							montoRecibido = Float.parseFloat( text.getText().trim() );
							close();
						}
						break;
				}
			}
		} );
		btn5.addSelectionListener( new SelectionAdapter() {
			@Override
			public void widgetSelected( SelectionEvent arg0 ) {
				text.append( "5" );
			}
		} );
		btn5.setText( "5" );

		btn4 = new Button( composite, SWT.NONE );
		btn4.setBounds( 10, 129, 68, 68 );
		btn4.setFont( SWTResourceManager.getFont( "Tahoma", 16, SWT.BOLD ) );
		btn4.addKeyListener( new KeyAdapter() {
			@Override
			public void keyPressed( KeyEvent e ) {
				switch( e.keyCode ) {
					case WidgetsUtil.TECLA_ESC:
						montoRecibido = null;
						close();
						break;

					case WidgetsUtil.TECLA_ENTER:
						if( !text.getText().isEmpty() ) {
							montoRecibido = Float.parseFloat( text.getText().trim() );
							close();
						}
						break;
				}
			}
		} );
		btn4.addSelectionListener( new SelectionAdapter() {
			@Override
			public void widgetSelected( SelectionEvent arg0 ) {
				text.append( "4" );
			}
		} );
		btn4.setText( "4" );

		btn7 = new Button( composite, SWT.NONE );
		btn7.setBounds( 10, 203, 68, 68 );
		btn7.setFont( SWTResourceManager.getFont( "Tahoma", 16, SWT.BOLD ) );
		btn7.addKeyListener( new KeyAdapter() {
			@Override
			public void keyPressed( KeyEvent e ) {
				switch( e.keyCode ) {
					case WidgetsUtil.TECLA_ESC:
						montoRecibido = null;
						close();
						break;

					case WidgetsUtil.TECLA_ENTER:
						if( !text.getText().isEmpty() ) {
							montoRecibido = Float.parseFloat( text.getText().trim() );
							close();
						}
						break;
				}
			}
		} );
		btn7.addSelectionListener( new SelectionAdapter() {
			@Override
			public void widgetSelected( SelectionEvent arg0 ) {
				text.append( "7" );
			}
		} );
		btn7.setText( "7" );

		btn8 = new Button( composite, SWT.NONE );
		btn8.setBounds( 84, 203, 68, 68 );
		btn8.setFont( SWTResourceManager.getFont( "Tahoma", 16, SWT.BOLD ) );
		btn8.addKeyListener( new KeyAdapter() {
			@Override
			public void keyPressed( KeyEvent e ) {
				switch( e.keyCode ) {
					case WidgetsUtil.TECLA_ESC:
						montoRecibido = null;
						close();
						break;

					case WidgetsUtil.TECLA_ENTER:
						if( !text.getText().isEmpty() ) {
							montoRecibido = Float.parseFloat( text.getText().trim() );
							close();
						}
						break;
				}
			}
		} );
		btn8.addSelectionListener( new SelectionAdapter() {
			@Override
			public void widgetSelected( SelectionEvent arg0 ) {
				text.append( "8" );
			}
		} );
		btn8.setText( "8" );

		btn9 = new Button( composite, SWT.NONE );
		btn9.setBounds( 158, 203, 68, 68 );
		btn9.setFont( SWTResourceManager.getFont( "Tahoma", 16, SWT.BOLD ) );
		btn9.addKeyListener( new KeyAdapter() {
			@Override
			public void keyPressed( KeyEvent e ) {
				switch( e.keyCode ) {
					case WidgetsUtil.TECLA_ESC:
						montoRecibido = null;
						close();
						break;

					case WidgetsUtil.TECLA_ENTER:
						if( !text.getText().isEmpty() ) {
							montoRecibido = Float.parseFloat( text.getText().trim() );
							close();
						}
						break;
				}
			}
		} );
		btn9.addSelectionListener( new SelectionAdapter() {
			@Override
			public void widgetSelected( SelectionEvent arg0 ) {
				text.append( "9" );
			}
		} );
		btn9.setText( "9" );

		btnC = new Button( composite, SWT.NONE );
		btnC.setBounds( 158, 277, 68, 68 );
		btnC.setFont( SWTResourceManager.getFont( "Tahoma", 16, SWT.BOLD ) );
		btnC.addKeyListener( new KeyAdapter() {
			@Override
			public void keyPressed( KeyEvent e ) {
				switch( e.keyCode ) {
					case WidgetsUtil.TECLA_ESC:
						montoRecibido = null;
						close();
						break;

					case WidgetsUtil.TECLA_ENTER:
						if( !text.getText().isEmpty() ) {
							montoRecibido = Float.parseFloat( text.getText().trim() );
							close();
						}
						break;
				}
			}
		} );
		btnC.addSelectionListener( new SelectionAdapter() {
			@Override
			public void widgetSelected( SelectionEvent arg0 ) {
				text.setText( "" );
				btnAceptar.setEnabled( false );
			}
		} );
		btnC.setText( "C" );

		btn0 = new Button( composite, SWT.NONE );
		btn0.setBounds( 84, 277, 68, 68 );
		btn0.setFont( SWTResourceManager.getFont( "Tahoma", 16, SWT.BOLD ) );
		btn0.addKeyListener( new KeyAdapter() {
			@Override
			public void keyPressed( KeyEvent e ) {
				switch( e.keyCode ) {
					case WidgetsUtil.TECLA_ESC:
						montoRecibido = null;
						close();
						break;

					case WidgetsUtil.TECLA_ENTER:
						if( !text.getText().isEmpty() ) {
							montoRecibido = Float.parseFloat( text.getText().trim() );
							close();
						}
						break;
				}
			}
		} );
		btn0.addSelectionListener( new SelectionAdapter() {
			@Override
			public void widgetSelected( SelectionEvent arg0 ) {
				if( !(text.getText().trim().isEmpty() || text.getText().trim().equals( "0" )) ) {
					text.append( "0" );
				}
			}
		} );
		btn0.setText( "0" );

		btnPunto = new Button( composite, SWT.NONE );
		btnPunto.setBounds( 10, 277, 68, 68 );
		btnPunto.setFont( SWTResourceManager.getFont( "Tahoma", 16, SWT.BOLD ) );
		btnPunto.addKeyListener( new KeyAdapter() {
			@Override
			public void keyPressed( KeyEvent e ) {
				switch( e.keyCode ) {
					case WidgetsUtil.TECLA_ESC:
						montoRecibido = null;
						close();
						break;

					case WidgetsUtil.TECLA_ENTER:
						if( !text.getText().isEmpty() ) {
							montoRecibido = Float.parseFloat( text.getText().trim() );
							close();
						}
						break;
				}
			}
		} );
		btnPunto.addSelectionListener( new SelectionAdapter() {
			@Override
			public void widgetSelected( SelectionEvent arg0 ) {
				if( (!(text.getText().trim().isEmpty() || text.getText().trim().equals( "0" ))) && !text.getText().contains( "." ) ) {
					text.append( "." );
				}
			}
		} );
		btnPunto.setText( "." );

		btnDel = new Button( composite, SWT.NONE );
		btnDel.setBounds( 10, 351, 68, 46 );
		btnDel.setImage( SWTResourceManager.getImage( VentasPrincipalCalculadorShell.class, "/com/epsilon/commons/util/images/arrow-left.png" ) );

		btnAceptar = new Button( composite, SWT.NONE );
		btnAceptar.setBounds( 84, 351, 142, 46 );
		btnAceptar.addKeyListener( new KeyAdapter() {
			@Override
			public void keyPressed( KeyEvent e ) {
				switch( e.keyCode ) {
					case WidgetsUtil.TECLA_ESC:
						montoRecibido = null;
						close();
						break;

					case WidgetsUtil.TECLA_ENTER:
						if( !text.getText().isEmpty() ) {
							montoRecibido = Float.parseFloat( text.getText().trim() );
							close();
						}
						break;
				}
			}
		} );
		btnAceptar.addSelectionListener( new SelectionAdapter() {
			@Override
			public void widgetSelected( SelectionEvent arg0 ) {
				montoRecibido = Float.parseFloat( text.getText().trim() );
				close();
			}
		} );
		btnAceptar.setEnabled( false );
		btnAceptar.setText( "Aceptar" );
		btnDel.addKeyListener( new KeyAdapter() {
			@Override
			public void keyPressed( KeyEvent e ) {
				if( WidgetsUtil.TECLA_ESC == e.keyCode ) {
					montoRecibido = null;
					close();
				}
			}
		} );
		btnDel.addSelectionListener( new SelectionAdapter() {
			@Override
			public void widgetSelected( SelectionEvent arg0 ) {
				if( !text.getText().trim().isEmpty() ) {
					String resp = text.getText().trim().substring( 0, text.getText().trim().length() - 1 );
					text.setText( "" );
					text.setText( resp );
				}

				if( !text.getText().trim().isEmpty() )
					btnAceptar.setEnabled( true );
				else
					btnAceptar.setEnabled( false );
			}
		} );
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText( "Recibido" );
		setSize( 244, 434 );

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	public Float getMontoRecibido() {
		return montoRecibido;
	}
}